//app.js
const comid = "JLBRYSJZ"
const nzid = "JLBRYSJZ"
const cnns = "RGF0YSBTb3VyY2U9NDcuOTIuMTY0LjE2NywxODc4XFRTRERCUztJbml0aWFsIENhdGFsb2c9dHNkamk7VXNlciBJRD10c2Q7UGFzc3dvcmQ9NEZERDc3MzVUMzEyRFM0ODFDRDkwaDJkMkE5RDQ2RDFDOXk4O3Bvb2xpbmc9dHJ1ZTttYXggcG9vbCBzaXplPTMyNzY3O0Nvbm5lY3QgVGltZW91dD02MDA="
const appId = 'wx073e769496954b77'
const secret = '95b2ef821a805d9cbba3c15ec97ae876'
const mchid = '' //直接入账客户提供，平台入账客户不填
const openid = ""
const url = "http://www.tianshangding.cn/"
const ApiUrl = "https://www.tianshangding.net/XcxWebService/TsdXcx.asmx/"
const XIAOQU_URL = "https://www.tianshangding.net/XcxWebService/TsdXiaoqu.asmx/"
const xcxhttps="https://xcx.tianshangding.net/tsdxcx/"
const logo = url + "TsdXcx/img/logo/" + comid + "/" + "logo.jpg"
//appid=wx073e769496954b77&appsecret=95b2ef821a805d9cbba3c15ec97ae876&openid=olHFL5CYSO27ENn6Nrr-9DH3KPAE
App({

  onLaunch: function() {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({

      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var code = res.code; //返回code

        var that = this;
        wx.request({
          url: that.globalData.ApiUrl + 'GetOpenid',
          data: {
            appid: that.globalData.appid,
            secret: that.globalData.secret,
            code: code
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded;'
          },
          success: function(res) {
            console.log(res.data);
            that.globalData.openid = res.data.openid //返回openid
          }
        })
      }
    })


    // 获取用户信息
    wx.getSetting({
      success: res => {

        if (res.authSetting['scope.userInfo']) {

          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({

            success: res => {
              console.log(res);
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo;

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })


  },

  //获取用户地理,并授权位置权限
  chooseLocation: function (obj) {
    wx.chooseLocation({
      success: function (res) {
        obj.setData({
          location: res,      //调用成功直接设置地址
          address:res.address
        })
      },
      fail: function () {
        wx.getSetting({
          success: function (res) {
            var statu = res.authSetting;
            if (!statu['scope.userLocation']) {
              wx.showModal({
                title: '是否授权当前位置',
                content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
                success: function (tip) {
                  if (tip.confirm) {
                    wx.openSetting({
                      success: function (data) {
                        if (data.authSetting["scope.userLocation"] === true) {
                          wx.showToast({
                            title: '授权成功',
                            icon: 'success',
                            duration: 1000
                          })
                          //授权成功之后，再调用chooseLocation选择地方
                          wx.chooseLocation({
                            success: function (res) {
                              obj.setData({
                                location: res,
                                address:res.address
                              })
                            },
                          })
                        } else {
                          wx.showToast({
                            title: '授权失败',
                            icon: 'success',
                            duration: 1000
                          })
                        }
                      }
                    })
                  }
                }
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '调用授权窗口失败',
              icon: 'success',
              duration: 1000
            })
          }
        })
      }
    })
  },


  globalData: {
    userInfo: {},
    url: url,
    comid: comid,
    nzid: nzid,
    ApiUrl: ApiUrl, XIAOQU_URL: XIAOQU_URL,xcxhttps:xcxhttps,
    cnns: cnns,
    openid: openid,
    appid: appId, 
    secret: secret,
    mchid: mchid,logo:logo
  }
})
